package com.ayoka.mvp;


import android.support.v4.app.FragmentManager;

import com.ayoka.R;
import com.ayoka.base.BasePresenter;
import com.ayoka.base.IBaseView;
import com.ayoka.bean.BaseResponse;
import com.ayoka.bean.ResponseCoursePackageDetailBean;
import com.ayoka.http.API;
import com.ayoka.http.OkHttpListener;
import com.ayoka.widget.TwoButtonDialog;
import com.jcp.baselibrary.dialog.BaseDialogFragment;
import com.jcp.baselibrary.dialog.DialogResultListener;
import com.jcp.baselibrary.utils.UIUtils;

/**
 * Created by gabriel on 2017/3/5.
 */

public interface CoursePackageMVP {

    interface CoursePackageModel {
    }

    interface ICoursePackageView extends IBaseView {

        void configCoursePackageDetailData(ResponseCoursePackageDetailBean coursePackageBean);

        void onUseCouponCallback(boolean isSuccess);
    }

    class CoursePackagePresenter extends BasePresenter<ICoursePackageView> {


        public void getCoursePackageDetailData(long coursePackageId) {
            mViewRef.get().showWaitDialog(UIUtils.getString(R.string.loading));
            API.getCoursePackageDetailData(this, coursePackageId, new OkHttpListener<BaseResponse<ResponseCoursePackageDetailBean>>() {
                @Override
                public void onSuccess(BaseResponse<ResponseCoursePackageDetailBean> response) {
                    if (!isAttached()) return;
                    mViewRef.get().hideWaitDialog();
                    mViewRef.get().configCoursePackageDetailData(response.data);
                }

                @Override
                public void onFailure(int errorCode, String errorMessage) {
                    if (!isAttached()) return;
                    mViewRef.get().hideWaitDialog();
                    mViewRef.get().showToast(errorMessage);
                }
            });
        }

        public void showUseCouponDialog(FragmentManager fragmentManager, final long couponID, String description) {
            TwoButtonDialog dialog = new TwoButtonDialog.Builder()
                    .setTitle(UIUtils.getString(R.string.point_out))
                    .setMessage(description)
                    .setNegativeText(UIUtils.getString(R.string.do_not_use))
                    .setPositiveText(UIUtils.getString(R.string.use))
                    .build();
            dialog.setDialogResultListener(new DialogResultListener<Boolean>() {
                @Override
                public void result(BaseDialogFragment dialogFragment, Boolean result) {
                    if (!isAttached()) return;
                    if (result) {
                        confirmUseCoupon(couponID);
                    }
                }
            });
            dialog.show(fragmentManager, "TransferConfirmDialog");
        }

        private void confirmUseCoupon(long couponID) {
            mViewRef.get().showWaitDialog(UIUtils.getString(R.string.loading));
            API.confirmUseCoupon(this, couponID, new OkHttpListener<BaseResponse>() {
                @Override
                public void onSuccess(BaseResponse response) {
                    if (!isAttached()) return;
                    mViewRef.get().hideWaitDialog();
                    mViewRef.get().showToast(response.errmsg);
                    mViewRef.get().onUseCouponCallback(true);
                }

                @Override
                public void onFailure(int errorCode, String errorMessage) {
                    if (!isAttached()) return;
                    mViewRef.get().hideWaitDialog();
                    mViewRef.get().showToast(errorMessage);
                    mViewRef.get().onUseCouponCallback(false);
                }
            });
        }
    }

}
